<template>
  <div class="news-add-or-update">
    <el-drawer :title="this.rowData ? '编辑' : '新增'" :visible.sync="drawer" custom-class="demo-drawer" direction="rtl" size="88%">
      <div class="demo-drawer__content">
        <div :style="cardHeightStyle" class="rightScrollNone">
          <el-form size="mini" :model="dataForm" :rules="rules" ref="vendorRuleForm">
            <el-row>
              <el-col :span="24">
                <el-form-item label="主题：" prop="title" :label-width="formLabelWidth">
                  <el-input v-model="dataForm.title" type="text" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="图片：" prop="url">
                  <el-upload :before-upload="beforeAvatarUpload" :http-request="UploadImage" :limit="9" :on-remove="handleRemove" :on-success="handleAvatarSuccess" :show-file-list="false" action="string" class="avatar-uploader avatar-uploadermine">
                    <img :src="dataForm.url" class="avatar" v-if="dataForm.url" />
                    <i class="el-icon-plus avatar-uploader-icon" v-else></i>
                  </el-upload>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="跳转连接：" prop="link" :label-width="formLabelWidth">
                  <el-input v-model="dataForm.link" type="text" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="是否启用：" prop="status" :label-width="formLabelWidth">
                  <el-radio-group v-model="dataForm.status">
                    <el-radio :label="1">启用</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item :label-width="formLabelWidth" label="排序：" prop="order">
                  <el-input autocomplete="off" type="number" v-model="dataForm.order"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="demo-drawer__footer">
          <el-button size="mini" @click="cancelForm">取 消</el-button>
          <el-button size="mini" :loading="loading" @click="dataFormSubmit" type="primary">确定</el-button>
          <!--          <el-button size="mini" :loading="loading" @click="dataFormSubmit(1)" type="success">存为草稿</el-button>-->
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { upfile } from '../../../api/baseInfo'
import { AddBannerApi, EditBannerApi } from '../../../api/banner'

export default {
  data() {
    const generateData = (_) => {
      const data = []
      for (let i = 1; i <= 15; i++) {
        data.push({
          key: i,
          label: `备选项 ${i}`,
          disabled: i % 4 === 0,
        })
      }
      return data
    }
    return {
      areaIdArr: [],
      mainBodyArr: [],
      data: generateData(),
      qyIdArr: [],
      renderFunc(h, option) {
        return (
          <span>
            {option.key} - {option.label}
          </span>
        )
      },
      dataForm: {
        id: '',
        status: 1,
        url: '',
        title: '',
        link: '',
        order: 1,
      },
      rowData: null,
      formLabelWidth: '120px',
      loading: false,
      drawer: false,
      rules: {
        title: [{ required: true, message: '请输入主题', trigger: 'blur' }],
        url: [{ required: true, message: '请上传图片', trigger: 'blur' }],
        status: [{ required: true, message: '请选择启用状态', trigger: 'blur' }],
      },
      dialogImageUrl: '',
      dialogVisible: false,
    }
  },
  mounted() {
    // this.getMainBodiesFun();
    // this.getAreaArrFun();
  },
  computed: {
    cardHeightStyle() {
      let documentClientHeight = document.body.clientHeight
      const height = documentClientHeight - 80 - 80
      return {
        height: height + 'px',
        'overflow-y': 'scroll',
        'padding-right': '20px;',
      }
    },
  },
  methods: {
    UploadImage(param) {
      let _this = this
      const formData = new FormData()
      formData.append('file', param.file)
      upfile(formData)
        .then((res) => {
          if (res.code == 200) {
            _this.dataForm.url = res.result
            param.onSuccess()
          }
        })
        .catch((response) => {
          param.onError()
        })
    },
    handleRemove(file, fileList) {
      this.dataForm.url = ''
    },
    handleAvatarSuccess(res, file) {},
    beforeAvatarUpload(file) {
      // 可在此进行图片大小限制
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过10MB!')
      }
      return isLt2M
    },

    // handleChange(value, direction, movedKeys) {
    //   console.log(value, direction, movedKeys);
    //   console.log("value4", this.value4)
    // },
    // changeMainBodyId(){
    //   this.getAreaArrFun()
    // },
    // changeAreaId(){
    //   KeHuArrApi({areaId: this.dataForm.areaId_id}).then(res => {
    //     this.qyIdArr = res.result;
    //   })
    // },
    // getAreaArrFun(){
    //   areaArrApi({main_body_id: this.dataForm.mainBodyId_id}).then(res => {
    //     this.areaIdArr = res.result;
    //   })
    // },
    // getMainBodiesFun() {
    //   mainBodiesApi({}).then(res => {
    //     this.mainBodyArr = res.result;
    //   })
    // },
    // handleRemove(file, fileList) {
    //   console.log(file, fileList);
    // },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    editorValChangHandle(content) {
      this.dataForm.desc = content
    },
    reportAdd(params) {
      return AddBannerApi(params)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success('保存成功')
          }
        })
        .catch((res) => {
          this.$message.error('保存失败')
        })
    },
    reportUpdate: function (params) {
      return EditBannerApi(params)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success('保存成功')
          }
        })
        .catch((res) => {
          this.$message.error('保存失败')
        })
    },

    dataFormSubmit() {
      this.$refs.vendorRuleForm.validate((flag) => {
        if (flag) {
          const data = this.dataForm
          var request = this.rowData ? this.reportUpdate(data) : this.reportAdd(data)
          request
            .then((d) => {
              this.cancelForm()
              this.$emit('refreshList')
            })
            .catch((d) => {
              this.cancelForm()
            })
        }
      })
    },
    cancelForm() {
      this.cleanInfo()
      this.loading = false
      this.drawer = false
    },
    open(row, role) {
      if (this.drawer) {
        return
      }
      this.rowData = row || null
      this.drawer = true
      this.openHook()
    },
    clearData() {
      this.$refs.vendorRuleForm.resetFields()
    },
    cleanInfo() {
      clearTimeout(this.timeOutCount)
    },
    openHook() {
      this.$nextTick((a) => {
        this.clearData()
        // this.$refs.quillEdit.open(this.rowData);
        if (this.rowData) {
          // 修改
          for (let key in this.dataForm) {
            this.dataForm[key] = this.rowData[key]
          }
        } else {
          this.loading = false
        }
      })
    },
  },
}
</script>
<style lang="scss">
.news-add-or-update {
  .icon-m {
    font-size: 25px;
    margin-left: 10px;

    position: relative;
    top: 4px;
    color: #03a9f4;
    cursor: pointer;
  }

  .el-tag {
    position: relative;
    right: 5px;
    z-index: 1;
    height: 40px;
    line-height: 40px;
    border-radius: 0 5px 5px 0;
    padding-left: 15px;
  }

  .my-email {
    .el-input__inner {
      border-radius: 5px 0 0 5px;
    }
  }
}

/*

  */

.fileInput {
  padding-right: 0;
}
</style>
